import Joi from 'joi';
import { commonValidation } from './common';

/**
 * 用户注册验证模式
 */
export const userRegisterSchema = Joi.object({
  username: commonValidation.username,
  email: commonValidation.email,
  password: commonValidation.password,
  confirmPassword: commonValidation.confirmPassword,
  phone: commonValidation.chinesePhone.optional(),
  firstName: Joi.string()
    .min(1)
    .max(50)
    .required()
    .messages({
      'string.min': '名字不能为空',
      'string.max': '名字长度不能超过50位',
      'any.required': '名字不能为空',
    }),
  lastName: Joi.string()
    .min(1)
    .max(50)
    .required()
    .messages({
      'string.min': '姓氏不能为空',
      'string.max': '姓氏长度不能超过50位',
      'any.required': '姓氏不能为空',
    }),
});

/**
 * 用户登录验证模式
 */
export const userLoginSchema = Joi.object({
  email: commonValidation.email,
  password: Joi.string()
    .required()
    .messages({
      'any.required': '密码不能为空',
    }),
  rememberMe: Joi.boolean()
    .default(false)
    .messages({
      'boolean.base': '记住我必须是布尔值',
    }),
});

/**
 * 用户更新验证模式
 */
export const userUpdateSchema = Joi.object({
  username: commonValidation.username.optional(),
  email: commonValidation.email.optional(),
  phone: commonValidation.chinesePhone.optional(),
  firstName: Joi.string()
    .min(1)
    .max(50)
    .optional()
    .messages({
      'string.min': '名字不能为空',
      'string.max': '名字长度不能超过50位',
    }),
  lastName: Joi.string()
    .min(1)
    .max(50)
    .optional()
    .messages({
      'string.min': '姓氏不能为空',
      'string.max': '姓氏长度不能超过50位',
    }),
  bio: Joi.string()
    .max(500)
    .allow('')
    .optional()
    .messages({
      'string.max': '个人简介长度不能超过500位',
    }),
  avatar: commonValidation.url.optional(),
  dateOfBirth: commonValidation.date.optional(),
  gender: Joi.string()
    .valid('male', 'female', 'other', 'prefer_not_to_say')
    .optional()
    .messages({
      'any.only': '性别值无效',
    }),
  location: Joi.string()
    .max(100)
    .optional()
    .messages({
      'string.max': '位置信息长度不能超过100位',
    }),
  website: commonValidation.url.optional(),
});

/**
 * 密码修改验证模式
 */
export const passwordChangeSchema = Joi.object({
  currentPassword: Joi.string()
    .required()
    .messages({
      'any.required': '当前密码不能为空',
    }),
  newPassword: commonValidation.password,
  confirmNewPassword: Joi.string()
    .valid(Joi.ref('newPassword'))
    .required()
    .messages({
      'any.only': '确认密码与新密码不匹配',
      'any.required': '确认新密码不能为空',
    }),
});

/**
 * 密码重置验证模式
 */
export const passwordResetSchema = Joi.object({
  email: commonValidation.email,
});

/**
 * 密码重置确认验证模式
 */
export const passwordResetConfirmSchema = Joi.object({
  token: Joi.string()
    .required()
    .messages({
      'any.required': '重置令牌不能为空',
    }),
  newPassword: commonValidation.password,
  confirmNewPassword: Joi.string()
    .valid(Joi.ref('newPassword'))
    .required()
    .messages({
      'any.only': '确认密码与新密码不匹配',
      'any.required': '确认新密码不能为空',
    }),
});

/**
 * 邮箱验证模式
 */
export const emailVerificationSchema = Joi.object({
  token: Joi.string()
    .required()
    .messages({
      'any.required': '验证令牌不能为空',
    }),
});

/**
 * 重新发送邮箱验证码模式
 */
export const resendVerificationSchema = Joi.object({
  email: commonValidation.email,
});

/**
 * 用户列表查询验证模式
 */
export const userListQuerySchema = Joi.object({
  page: commonValidation.page,
  limit: commonValidation.limit,
  sortBy: Joi.string()
    .valid('createdAt', 'updatedAt', 'username', 'email', 'status')
    .default('createdAt')
    .messages({
      'any.only': '排序字段无效',
    }),
  sortOrder: commonValidation.sortOrder,
  keyword: commonValidation.keyword,
  status: Joi.string()
    .valid('active', 'inactive', 'suspended', 'pending', 'deleted')
    .optional()
    .messages({
      'any.only': '状态筛选值无效',
    }),
  role: Joi.string()
    .valid('admin', 'user', 'moderator', 'guest')
    .optional()
    .messages({
      'any.only': '角色筛选值无效',
    }),
  createdAfter: commonValidation.date.optional(),
  createdBefore: commonValidation.date.optional(),
});

/**
 * 用户状态更新验证模式
 */
export const userStatusUpdateSchema = Joi.object({
  status: Joi.string()
    .valid('active', 'inactive', 'suspended', 'deleted')
    .required()
    .messages({
      'any.only': '状态值无效',
      'any.required': '状态不能为空',
    }),
  reason: Joi.string()
    .max(500)
    .optional()
    .messages({
      'string.max': '原因说明长度不能超过500位',
    }),
});

/**
 * 用户角色更新验证模式
 */
export const userRoleUpdateSchema = Joi.object({
  role: Joi.string()
    .valid('admin', 'user', 'moderator', 'guest')
    .required()
    .messages({
      'any.only': '角色值无效',
      'any.required': '角色不能为空',
    }),
});

/**
 * 用户头像上传验证模式
 */
export const avatarUploadSchema = Joi.object({
  avatar: Joi.object({
    fieldname: Joi.string().required(),
    originalname: Joi.string().required(),
    encoding: Joi.string().required(),
    mimetype: Joi.string()
      .valid('image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp')
      .required()
      .messages({
        'any.only': '只支持 JPEG、PNG、GIF、WebP 格式的图片',
        'any.required': '文件类型不能为空',
      }),
    size: Joi.number()
      .max(5 * 1024 * 1024) // 5MB
      .required()
      .messages({
        'number.max': '头像文件大小不能超过5MB',
        'any.required': '文件大小不能为空',
      }),
    buffer: Joi.binary().required(),
  }).required().messages({
    'any.required': '请选择头像文件',
  }),
});